import path from "./path";
import Instance from './Utils/request'; // 导入axios实例

const api = {


//#region AI聊天接口
//  // 初始化AI聊天会话
  startChatSession: (userId) => {
    return Instance.get(`/api/ai-server/start/${userId}`);
  },

  // 发送AI聊天消息
  sendMessage: (data) => {
    return Instance.post(`/api/ai-server/send`, data);
  },

//#endregion


//#region 用户接口
  // 用户登录
  login: async (userAccountNumber, userPassword) => {
    try {
      const params = new URLSearchParams();
      params.append('userAccountNumber', userAccountNumber);
      params.append('userPassword', userPassword);
      const response = await Instance.post(`/api/user-server/login`, params, {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
      });
      return response;
    } catch (error) {
      console.error('Login error details:', {
        status: error.response?.status,
        statusText: error.response?.statusText,
        data: error.response?.data,
        headers: error.response?.headers
      });
      throw error;
    }
  },

  // 用户注册
  register: (userData) => {
    return Instance.post(`/api/user-server/addUser`, userData);
  },

  // 修改用户信息
  updateUserById: (id, userDTO, avatarFile) => {
    const formData = new FormData();
    // Send userDTO as a JSON string with key 'user'
    formData.append('user', new Blob([JSON.stringify(userDTO)], { type: 'application/json' }));
    if (avatarFile) {
      formData.append('avatar', avatarFile);
    }
    return Instance.put(`/api/user-server/updateUserById/${id}`, formData);
  },
  
  //#endregion


  //#region 汽车品牌

  // 获取所有汽车品牌
  getCarBrands: () => {
    return Instance.get(`/api/model-server/getAllBrands`);
  },
  //#endregion


  //#region 汽车
 // 多条件查询车辆信息
  searchCarsByDTO: (modelDTO) => {
    return Instance.get(`/api/model-server/searchCarsByDTO`, {
      params: modelDTO
    });
  },

  
  //#endregion

  //#region 车
  // 查询车型信息
  getAllModelType: () => {
    return Instance.get(`/api/model-server/getAllModelType`);
  },

  // 查询车部件
  getCarSpecs: (modelId) => {
    return Instance.get(`/api/model-server/searchByModelId`, {
      params: { modelId }
    });
  },

  // 查询车身数据
  

  // 根据车型ID查询车部件信息
  getCarParts: (modelId) => {
    return Instance.get(`/api/model-server/getCarPartByModelId`, {
      params: { modelId }
    });
  },

  
 //#endregion

 //#region 评论

 // 根据车型ID查询评论信息
  getModelComments: (modelId) => {
    return Instance.get(`/api/user-server/getModelComments/${modelId}`);
  },

  // 添加评论
  addComment: (comment) => {
    return Instance.post('/api/user-server/addComment', comment);
  }

};


export default api;
